<?php echo $this->render('/public/header'); ?>
<div class="col-xs-12" id="main">
    <a href="/sell/purchase_add" class="btn btn-success">添加采购</a>
    <br/>
    <br/>
    <form>
        <div class="form-inline">
            <div class="form-group">
                <select class="form-control" name="job_number">
                    <option value="0">--请选择--</option>
                    <?php foreach ($this->params['staff'] as $k => $v): ?>
                        <option <?php echo yii::$app->request->get('job_number') == $v['job_number'] ? 'selected' : ''; ?>
                                value="<?php echo $v['job_number']; ?>"><?php echo $v['name']; ?></option>
                    <?php endforeach;?>
                </select>
            </div>
            <div class="form-group">
                <select class="form-control" name="pay_status">
                    <option value="0">付款状态</option>
                    <?php foreach (yii::$app->params['pay_status'] as $k2 => $v2): ?>
                        <option value="<?php echo $k2; ?>" <?php echo yii::$app->request->get('pay_status') == $k2 ? 'selected' : ''; ?>><?php echo $v2; ?></option>
                    <?php endforeach;?>
                </select>
            </div>

            <div class="form-group">
                <input type="text" name="purchase_num" value="<?php echo yii::$app->request->get('purchase_num'); ?>"
                       placeholder="搜索采购编号" class="form-control"/>
            </div>
            <div class="form-group">
                <input type="text" name="invoice" value="<?php echo yii::$app->request->get('invoice'); ?>"
                       placeholder="搜索invoice编号" class="form-control"/>
            </div>
            <div class="form-group">
                <select class="form-control" name="product_status">
                    <option value="0">货物状态</option>
                    <?php foreach (yii::$app->params['product_status'] as $k => $v): ?>
                        <option value="<?php echo $k; ?>" <?php echo yii::$app->request->get('product_status') == $k ? 'selected' : ''; ?>><?php echo $v; ?></option>
                    <?php endforeach;?>
                </select>
            </div>
            <div class="form-group">
                <input type="text" placeholder="最后付款时间" class="form-control form_datetime" name="last_modify"
                       value="<?php echo yii::$app->request->get('last_modify'); ?>"/>
            </div>
            <div class="form-group">
                <input size="16" type="text" name="product_status_time" class="form-control form_datetime"
                       placeholder="货物状态出入库时间" value="<?php echo yii::$app->request->get('product_status_time'); ?>">
            </div>
            <div class="form-group">
                <button class="btn btn-success">搜素</button>
            </div>
        </div>
    </form>
    <br/>
    <table class="table table-borderd">
        <thead>
        <tr>
            <th style="width:50px">采购</th>
            <th style="width:100px">业务员</th>
            <th style="width:110px">采购编号</th>
            <th style="width:110px">采购序号</th>
            <th style="width:110px">invoice编号</th>
            <th>已付</th>
            <th>未付</th>
            <th style="width:80px">最后付款时间</th>
            <th style="width:80px">付款进度</th>
            <th style="width:80px">货物状态</th>
            <th style="width:120px">货物状态时间</th>
            <th style="width:85px">签订日期</th>
            <th style="width:85px">交货日期</th>
            <th style="width:85px">采购商品</th>
            <th style="width:100px">供应商</th>
            <th style="overflow: hidden">备注信息</th>
            <th style="width:80px">总金额</th>
            <th style="width:80px">定金</th>
            <th style="width:100px">操作</th>
        </tr>
        </thead>
        <tbody>
        <?php foreach ($this->params['list'] as $v): ?>
            <tr>
                <td>
                    <?php foreach ($this->params['staff'] as $v2): ?>
                        <?php if ($v['job_number'] == $v2['job_number']): ?>
                            <?php echo $v2['name']; ?>
                        <?php endif;?>
                    <?php endforeach;?>
                </td>
                <td>
                    <select class="form-control" onchange="update(<?php echo $v['id']; ?>,'service',this)">
                        <option value="0">请选择</option>
                        <?php foreach ($this->params['staff'] as $v2): ?>
                            <option <?php if ($v['service'] == $v2['job_number']) {
    echo 'selected';
}?> value="<?php echo $v2['job_number']; ?>"><?php echo $v2['name']; ?></option>
                        <?php endforeach;?>
                    </select>
                </td>
                <td><input onchange="update(<?php echo $v['id']; ?>,'purchase_num',this)" style="width:100%;border:none"
                           type="text" value="<?php echo $v['purchase_num']; ?>"/></td>
                <td>
                    <input onchange="update(<?php echo $v['id']; ?>,'s_num',this)" style="width:100%;border:none"
                           type="text" value="<?php echo $v['s_num']; ?>"/>
                </td>
                <td><input onchange="update(<?php echo $v['id']; ?>,'invoice',this)" style="width:100%;border:none"
                           type="text" value="<?php echo $v['invoice']; ?>"/></td>
                <td>
                    <?php if ($this->params['show']) {
    echo isset($this->params['purchase'][$v['purchase_num']]) ? "<a target='__blank' href='/table/bank?purchase_num={$v['purchase_num']}'>{$this->params['purchase'][$v['purchase_num']]['money']}</a>" : '';
} else {
    echo isset($this->params['purchase'][$v['purchase_num']]) ? $this->params['purchase'][$v['purchase_num']]['money'] : '';
}
;?>
                </td>
                <td><?php echo $v['total_fee'] - (int) (isset($this->params['purchase'][$v['purchase_num']]) ? $this->params['purchase'][$v['purchase_num']]['money'] : ''); ?></td>
                <td><?php echo isset($this->params['purchase'][$v['purchase_num']]) ? $this->params['purchase'][$v['purchase_num']]['last_modify'] : ''; ?></td>
                <td>
                    <select class="form-control" style="border:none;padding:0px;margin: 0"
                            onchange="update(<?php echo $v['id']; ?>,'pay_status',this)">
                        <?php foreach (yii::$app->params['pay_status'] as $k2 => $v2): ?>
                            <option value="<?php echo $k2; ?>" <?php echo $v['pay_status'] == $k2 ? 'selected' : ''; ?>><?php echo $v2; ?></option>
                        <?php endforeach;?>
                    </select>
                </td>
                <td>
                    <select class="form-control" name="product_status" style="border:none;padding:0px"
                            onchange="update(<?php echo $v['id']; ?>,'product_status',this)">
                        <option value="0">--请选择--</option>
                        <?php foreach (yii::$app->params['product_status'] as $k2 => $v2): ?>
                            <option value="<?php echo $k2; ?>" <?php echo $v['product_status'] == $k2 ? 'selected' : ''; ?> ><?php echo $v2; ?></option>
                        <?php endforeach;?>
                    </select>
                </td>
                <td><?php echo $v['product_status_time'] . yii::$app->params['product_status'][$v['product_status']]; ?></td>
                <td><?php echo $v['qianding_date']; ?></td>
                <td><?php echo $v['delivery_date']; ?></td>
                <td><?php
$detail = json_decode($v['product_list'], true);
$str    = '';
foreach ($detail as $v2) {
    $str .= $v2['item'];
}
echo $str;
?></td>
                <td><?php echo $v['supplier_name']; ?></td>
                <td style="overflow: hidden"><input value="<?php echo $v['remark2']; ?>"
                                                    onchange="update(<?php echo $v['id']; ?>,'remark2',this)"
                                                    type="text" class="form-control"/></td>
                <td><?php echo $v['total_fee']; ?></td>
                <td><?php echo $v['deposit']; ?></td>
                <td>
                    <div class="btn-group">
                        <ul class="dropdown-menu" style="margin-left:25px">
                            <?php if (isset($this->params['invoice_list'])): ;?>
                                    <?php foreach ($this->params['invoice_list'] as $v2): ;?>
                                            <li>
                                                <a href="javascript:bind_invoice(<?php echo $v['id']; ?>,'<?php echo $v2; ?>')"><?php echo $v2; ?></a>
                                            </li>
                                        <?php endforeach;?>
                                <?php endif;?>
                        </ul>
                    </div>
                    <a href="/sell/purchase_edit?id=<?php echo $v['id']; ?>">编辑</a>
                    <a href="/sell/download_purchase?id=<?php echo $v['id']; ?>" style="color:green">下载</a>
                    <?php if ($role == 1 || $role == 2): ;?><a onclick="del(<?php echo $v['id']; ?>)" style="color:red">删除</a><?php endif;?>
                </td>
            </tr>
        <?php endforeach;?>
        </tbody>
    </table>
    <div id="mypage"></div>
</div>

<script>
    $(function () {
        $(".form_datetime").datepicker({format: 'yyyy-mm-dd'});
    });

    function update(id, params, e) {
        const value = e.value;
        layer.confirm('确认修改', {}, function () {
            $.post('/sell/update-purchase-act', {id: id, param: params, value: value}, function (res) {
                if (res.code == 1) {
                    layer.confirm(res.msg, {}, function () {
                        window.location.reload();
                    })
                } else {
                    layer.alert(res.msg);
                }
            }, 'json')
        })
    }


    function bind_invoice(id, invoice) {
        if (layer.confirm('确认绑定', {}, function () {
                var url = '/sell/bind_invoice';
                $.post(url, {id: id, invoice: invoice}, function (res) {
                    if (res.code == 1) {
                        layer.confirm(res.msg, {}, function () {
                            window.location.reload();
                        })
                    } else {
                        layer.alert(res.msg);
                    }
                }, 'json')
            })) ;
    }

    function del(id) {
        layer.confirm('确认删除', {}, function () {
            var url = '/sell/purchase_del';
            $.post(url, {id: id}, function (res) {
                if (res.code == 1) {
                    layer.confirm(res.msg, {}, function () {
                        window.location.reload();
                    })
                } else {
                    layer.alert(res.msg);
                }
            }, 'json')
        })
    }
</script>
<script>
    layui.laypage({
        cont: "mypage",
        pages:<?php echo $this->params['page']['page']; ?>,
        curr: <?php echo $this->params['page']['current']; ?>,
        skin: "#5FB878",
        skip: true,
        jump: function (obj, first) {
            if (!first) {
                if (window.location.search) {
                    window.location.href = "<?php echo $this->params['page']['baseUrl']; ?>" + "&p=" + obj.curr;
                }
                else {
                    window.location.href = "<?php echo $this->params['page']['baseUrl']; ?>" + "?p=" + obj.curr;
                }
            }
        }
    })
</script>
<?php echo $this->render('/public/footer'); ?>
